//
//  BHCompetitionDoubleView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/11.
//

import UIKit

protocol BHCompetitionDoubleViewDelegate {
    func didChoseLeft(model: BHCommonSortTypeModel)
    func didChoseRight(model: BHCommonSortTypeModel)
    func didTypeChoseClose()
}

class BHCompetitionDoubleView: UIView {
    
    var delegate: BHCompetitionDoubleViewDelegate?
    
    var itemArray: [BHCompetitionAdditionModel] = [] {
        didSet {
           
            self.leftTable.reloadData()
            self.rightTable.reloadData()
        }
    }
    
    lazy var locationSecView: BHCompetitionAreaLocationView = {
        let view = BHCompetitionAreaLocationView.loadFromXib()
        return view
    }()
    
    lazy var leftTable: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.bounces = false
        return view
    }()
    
    lazy var rightTable: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .color_f8f8f8
        view.bounces = false
        return view
    }()
    
    // 赛事的筛选专属
    lazy var areaTable: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .color_f8f8f8
        view.bounces = false
        return view
    }()
    
    lazy var line: UIView = {
        let view = UIView.init()
        view.backgroundColor = .color_e7e7e7
        return view
    }()
    
    lazy var shadowLayerView: UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        return view
    }()
    
    var leftArray: [BHCommonSortTypeModel] = []{
        didSet {
            self.leftTable.reloadData()
            self.rightTable.reloadData()
        }
    }
    
    /// 0表示二级筛选  1表示赛事的三级筛选 
    fileprivate var type: Int = 0
    fileprivate var leftIndex: Int = 0
    fileprivate var cityIndex: Int = 0
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
   
    init(type: Int) {
        self.type = type
        super.init(frame: .zero)
        self.isHidden = true
        self.addSubview(self.shadowLayerView)
        self.addSubview(self.line)
        self.addSubview(self.leftTable)
        self.addSubview(self.rightTable)
        if type == 1 {
            self.addSubview(self.areaTable)
        }
        self.shadowLayerView.mas_makeConstraints { (make) in
            make?.left.right()?.top()?.bottom()?.equalTo()(self)
        }
        
        self.shadowLayerView.addViewAction {[weak self] (_) in
            self?.showOrHide(show: false)
            self?.delegate?.didTypeChoseClose()
        }
        
        self.line.mas_makeConstraints { (make) in
            make?.left.top()?.right().equalTo()(self)
            make?.height.mas_equalTo()(0.5)
        }
        
        if type == 0 {
            self.itemArray = BHCompetitionAdditionModel.classArray
            
            self.leftTable.mas_makeConstraints { (make) in
                make?.left.equalTo()(self)
                make?.height.mas_equalTo()(200)
                make?.top.mas_equalTo()(0.5)
                make?.width.mas_equalTo()(90)
            }
            
            self.rightTable.mas_makeConstraints { (make) in
                make?.left.mas_equalTo()(90)
                make?.top.mas_equalTo()(0.5)
                make?.right.equalTo()(self)
                make?.height.mas_equalTo()(200)
            }
        }else {
            self.itemArray = BHCompetitionAdditionModel.areaArray
            self.addSubview(self.locationSecView)
            self.locationSecView.mas_makeConstraints { (make) in
                make?.left.right()?.equalTo()(self)
                make?.top.mas_equalTo()(0.5)
                make?.height.mas_equalTo()(40)
            }
            
            self.leftTable.mas_makeConstraints { (make) in
                make?.left.equalTo()(self)
                make?.height.mas_equalTo()(200)
                make?.top.equalTo()(self.locationSecView.mas_bottom)
                make?.width.mas_equalTo()(90)
            }
            
            self.rightTable.mas_makeConstraints { (make) in
                make?.left.mas_equalTo()(90)
                make?.top.equalTo()(self.locationSecView.mas_bottom)
                make?.width.mas_equalTo()(100)
                make?.height.mas_equalTo()(200)
            }
            
            self.areaTable.mas_makeConstraints { (make) in
                make?.left.mas_equalTo()(190)
                make?.right.equalTo()(self)
                make?.top.equalTo()(self.locationSecView.mas_bottom)
                make?.height.mas_equalTo()(200)
            }
        }
        
    }
    
}

// animation
extension BHCompetitionDoubleView {
    func showOrHide(show: Bool) {
        self.isHidden = !show
    }
   
}


extension BHCompetitionDoubleView: UITableViewDataSource {
   
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.leftTable {
            return self.leftArray.count
        }else {
            if self.type == 0 {
                if self.leftArray.count == 0 {
                    return 0
                }
                let model = self.leftArray[self.leftIndex]
                return model.childArray.count
            }else {
                if self.leftArray.count == 0 {
                    return 0
                }
                let model = self.leftArray[self.leftIndex]
                if model.childArray.count == 0 {
                    return 0
                }
                let areaModel = model.childArray[self.cityIndex]
                return areaModel.areaArray.count
            }
            
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHHomeChoseCityCell", for: indexPath) as! BHHomeChoseCityCell
        if tableView == leftTable {
            let model = self.leftArray[indexPath.row]
            cell.text_label.text = model.text
            cell.canChose = true
            cell.chose = indexPath.row == self.leftIndex
        }else {
            if self.type == 0 {
                let leftModel = self.leftArray[self.leftIndex]
                let model = leftModel.childArray[indexPath.row]
                cell.text_label.text = model.text
                cell.canChose = false
            }else {
                if tableView == self.rightTable {
                    let leftModel = self.leftArray[self.leftIndex]
                    let model = leftModel.childArray[indexPath.row]
                    cell.text_label.text = model.text
                    cell.canChose = true
                }else {
                    let leftModel = self.leftArray[self.leftIndex]
                    let model = leftModel.childArray[self.cityIndex]
                    let areaModel = model.areaArray[indexPath.row]
                    cell.text_label.text = areaModel.name
                    cell.canChose = false
                }
            }
        }
        return cell
    }
}

extension BHCompetitionDoubleView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == self.leftTable {
            let model = self.leftArray[indexPath.row]
            if model.childArray.count == 0 {
                self.delegate?.didChoseLeft(model: model)
            }
            self.leftIndex = indexPath.row
            self.leftTable.reloadData()
            self.rightTable.reloadData()
        }else {
            
            let leftModel = self.leftArray[self.leftIndex]
            let model = leftModel.childArray[indexPath.row]
            self.delegate?.didChoseRight(model: model)
            
            self.showOrHide(show: false)
            self.delegate?.didTypeChoseClose()
        }
    }
}
